import { getUserInfo, setUserInfo } from "../../utils/storage"
import config from '../../config/index'
import { reduceCountByChannel, reduceCountByClue, reduceCountByPeer } from '../../api/wxuser/index'
Component({
 
  properties: {
    callShow: {
      type: Boolean,
      value: false
    },
    key: {
      type: String
    },
    callId: {
      type: String
    },
    useCoin: {
      type: Number
    },
    reduceCoin: {
      type: Number
    }
  },
  data: {
    user: {} as WxUser,
  },
  lifetimes: {
    attached() {
      this.initData()
    }
  },
  methods: {
    async initData() {
      const user = getUserInfo()
      this.setData({
        user
      })
    },
    onClose() {
      this.setData({
        callShow: false
      })
      this.triggerEvent('show', this.properties.callShow)
    },
    reduce() {
      let data = {
        id: this.data.callId,
        userId: this.data.user.id,
        reduceCoin: this.properties.reduceCoin
      }
      let that = this
      switch(this.properties.key) {
        case 'channel':
          reduceCountByChannel(data).then((res) => {
            if(res.statusCode === config.CREATED_CODE) {
              setUserInfo(res.data)
              wx.showToast({
                title: '消耗成功',
                icon: 'success',
                success() {
                  that.onClose()
                  setTimeout(() => {
                    wx.reLaunch({
                      url: `/pages/personalChannelDetail/personalChannelDetail?id=${that.data.callId}`,
                      success() {
                        setTimeout(() => {
                          that.triggerEvent('showPhone')
                        }, 1000)
                      }
                    })
                  }, 1000)
                }
              })
            } else {
              wx.showToast({title: '网络错误', icon: 'error'})
            }
          })
          break
        case 'clue':
          reduceCountByClue(data).then((res) => {
            if(res.statusCode === config.CREATED_CODE) {
              setUserInfo(res.data)
              wx.showToast({
                title: '消耗成功',
                icon: 'success',
                success() {
                  that.onClose()
                  setTimeout(() => {
                    wx.reLaunch({
                      url: `/pages/newCarClueDetail/newCarClueDetail?id=${that.data.callId}`,
                      success() {
                        setTimeout(() => {
                          that.triggerEvent('showPhone')
                        }, 1000)
                      }
                    })
                  }, 1000)
                }
              })
            } else {
              wx.showToast({title: '网络错误', icon: 'error'})
            }
          })
          break
        case 'peer':
          reduceCountByPeer(data).then((res) => {
            if(res.statusCode === config.CREATED_CODE) {
              setUserInfo(res.data)
              wx.showToast({
                title: '消耗成功',
                icon: 'success',
                success() {
                  that.onClose()
                  setTimeout(() => {
                    wx.reLaunch({
                      url: `/pages/peerCarDetail/peerCarDetail?id=${that.data.callId}`,
                      success() {
                        setTimeout(() => {
                          that.triggerEvent('showPhone')
                        }, 1000)
                      }
                    })
                  }, 1000)
                }
              })
            } else {
              wx.showToast({title: '网络错误', icon: 'error'})
            }
          })
          break
        default:
          console.log('key不对');
          break
      }
    },
    showCall() {
      this.setData({
        callShow: false
      })
      this.triggerEvent('show', this.properties.callShow)
      this.triggerEvent('reLaunch')
      setTimeout(() => {
        this.triggerEvent('showPhone')
      }, 1000)
    }
  }
})
